


// Complier charateristics //





// Monotonicity

// Compliers


use "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv", clear 

cd  G:\Data\Workdata\707677\Common\03Output\02Regresssions




keep if time==1

tab obs_lawyer

egen lawyer_id_unique =group(lawyerid court) // Unique lawyer id within courts. some lawyers work in multiple courts
bys lawyer_id_unique: egen n_lawyer=count(lawyer_id_unique)
egen mean_lawyer_leniency= mean(granted), by(lawyer_id_unique)

gen iv_judge2_all = (mean_lawyer_leniency*n_lawyer - granted)/(n_lawyer-1)

gen iv_lawyer2=iv_judge2_all-(iv_court*obs_court-granted)/(obs_court-1)



keep if n_lawyer>=20

egen n_judge=nvals(lawyer_id_unique), by(court aar)



tab n_judge

keep if n_judge>1


egen income_groups_prior=xtile(erhvervsindk_mean), nq(2) by(pyear)
replace income_groups_prior= income_groups_prior-1




local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"



egen court_pyear =group(court pyear) 
tab court_pyear

tab time



// Judge leniency court x entry year FE


reg granted iv_lawyer2 `Xvar_short' i.court_pyear , cluster(lawyer_id_unique)

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg granted lawyer_residual  i.court_pyear , cluster(lawyer_id_unique)


gen beta_lawyer2=_b[lawyer_residual]

gen alpha_lawyer2=_b[_cons]

sort aar pnr

// Percentiles of instrument

egen p99_iv_lawyer2=pctile(lawyer_residual), p(99)

egen p1_iv_lawyer2=pctile(lawyer_residual), p(1)

egen p99_iv_judge2=pctile(lawyer_residual), p(99)

egen p1_iv_judge2=pctile(lawyer_residual), p(1)


// IV_lawyer_2

local complier_iv_lawyer2= beta_lawyer2*(p99_iv_lawyer2-p1_iv_lawyer2) // Define as local instead


putexcel set iv_compliers_chara, replace
putexcel A1="Charateristics"
putexcel B1="Full Sample"
putexcel C1="Granted"
putexcel D1="Compliers"
putexcel E1="Compliers_Lefgren"

gen age_0_45=(age0>=1 & age0<=45 & age0!=.)

local Xvar_short male age_0_40 age_0_45 employed_pstill_m1 unemployed_pstill_m1 legmarried_m1  housedum1  lower_secondary income_groups_prior




local row=2

foreach x of local Xvar_short {

putexcel A`row'="`x'"
	
sum `x'
local p_`x'=r(mean)
putexcel B`row'=`p_`x''	

sum `x' if granted==1
local p_`x'_g = r(mean)
putexcel C`row'=`p_`x'_g'

sum lawyer_residual if `x'==1, d
local z1=r(p1)
local z99=r(p99)
qui reg granted lawyer_residual i.court_pyear if `x'==1
local share_`x'=_b[lawyer_residual]*(`z99'-`z1')
local stat_`x'=(`share_`x''*`p_`x'')/`complier_iv_lawyer2'
putexcel D`row'=`stat_`x''

capture drop y_treat
gen y_treat= granted*`x'
ivreghdfe y_treat (granted=lawyer_residual) `Xvar_short', absorb(court_pyear) tol(1e-6) 
local complier_mean=_b[granted]
putexcel E`row'=`complier_mean'


local ++row
	
}

